<?php

namespace app\api\controller;

use think\Db;

/**
 * 技师控制器
 * @package app\index\controller
 */
class Statistics extends Auth
{
    public $noNeedLogin = [];

    public static $projectMoney = 0;//项目
    public static $projectCommission = 0;//项目提成
    public static $projectNumber = 0;//项目数量
    public static $projectRow = 0;//排钟
    public static $projectClock = 0;//点钟
    public static $projectCall = 0;//call数量
    public static $projectAdd = 0;//加钟数量
    public static $projectAddRow = 0;//加班排钟
    public static $projectAddClock = 0;//加班点钟
    public static $projectMan = 0;//主项
    public static $projectOther = 0;//副项

    public static $cardMoney = 0;//售卡金额
    public static $cardCommission = 0;//售卡提成
    public static $cardNumber = 0;//售卡数量

    public static $productMoney = 0;//商品金额
    public static $productCommission = 0;//商品提成
    public static $productNumber = 0;//商品数量

    public static $room_number = 0;//房间提成
    public static $user_number = 0;//客户数
    public static $averagePrice = 0;//客户均价
    public static $projectData = [];//项目数据
    public static $cardData = [];//卡数据
    public static $productData = [];//商品数据

    /**
     * 楼面个人业绩
     */
    public function userStatistics(){
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if(!isset($getData['type'])|| empty($getData['type']) ){
                $this->result('缺少时间类型', '', 204);
            }
            $userInfo = $this->userInfo;
            if($getData['type'] == '1') {
                $field = 'created_time';
            }else{
                $field = 'updated_time';
            }
            //下单时间
            if ($getData['time'] == '1') {
                //今天
                $time = 'today';
            }
            if ($getData['time'] == '2') {
                //昨天
                $time = 'yesterday';
            }
            if ($getData['time'] == '3') {
                //本月
                $time = 'month';
            }
            if ($getData['time'] == '4') {
                //上月
                $time = 'last month';
            }
            $data = Db::name('qt_commission_log')
                ->where(['store_id'=>$userInfo['store_id'],'cashier_id'=>$userInfo['id']])
                ->whereTime($field, $time)
                ->select();
            //处理数据
            foreach ($data as $key=>$value){
                //项目
                if($value['type'] == '1'){
                    self::$projectMoney += $value['money'];
                    self::$projectCommission += $value['receiver'];
                    self::$projectNumber += 1;
                    self::$projectData[] = $value;
                }
                //商品
                if($value['type'] == '2'){
                    self::$productMoney += $value['money'];
                    self::$productCommission += $value['receiver'];
                    $goodsNum = Db::name('qt_product_order_item')->where(['id'=>$value['order_project_id']])->value('goods_num');
                    self::$productNumber += $goodsNum;
                    self::$productData[] = $value;
                }
                //售卡
                if($value['type'] == '3' || $value['type'] == '4' || $value['type'] == '5'|| $value['type'] == '7'){
                    self::$cardMoney += $value['money'];
                    self::$cardCommission += $value['receiver'];
                    self::$cardNumber += 1;
                    self::$cardData[] = $value;
                }
            }
            self::$projectData = $this->getData(self::$projectData,'1',$userInfo);
            self::$productData = $this->getData(self::$productData,'2',$userInfo);
            self::$cardData = $this->getData(self::$cardData,'3',$userInfo);
            //用户数量
            self::$user_number =  Db::name('qt_commission_log')
                ->where(['store_id'=>$userInfo['store_id'],'cashier_id'=>$userInfo['id']])
                ->whereTime('created_time', $time)
                ->group('room_id')
                ->count();
            //房间数量
            self::$room_number = Db::name('qt_commission_log')
                ->where(['store_id'=>$userInfo['store_id'],'cashier_id'=>$userInfo['id']])
                ->whereTime('created_time', $time)
                ->group('room_id')
                ->count();
            //客单价
            $total = self::$projectMoney + self::$productMoney;
            if(self::$user_number == '0'){
                self::$averagePrice = '0';
            }else{
                self::$averagePrice = number_format(($total / self::$user_number),'2');
            }
            $result['projectMoney'] = self::$projectMoney;
            $result['projectCommission'] = self::$projectCommission;
            $result['projectNumber'] = self::$projectNumber;
            $result['cardMoney'] = self::$cardMoney;
            $result['cardCommission'] = self::$cardCommission;
            $result['cardNumber'] = self::$cardNumber;
            $result['productMoney'] = self::$productMoney;
            $result['productCommission'] = self::$productCommission;
            $result['productNumber'] = self::$productNumber;
            $result['room_number'] = self::$room_number;
            $result['user_number'] = self::$user_number;
            $result['averagePrice'] = self::$averagePrice;
            $result['projectData'] = self::$projectData;
            $projectData = $result['projectData']['data'];
            $projectData =  unique_2d_array_by_key($projectData,"name");
            foreach ($projectData as $key=>$value){
                $projectData[$key]['row'] = self::$projectData['row'];
                $projectData[$key]['clock'] = self::$projectData['clock'];
                $projectData[$key]['add'] = self::$projectData['add'];
                $projectData[$key]['call'] = self::$projectData['call'];
            }
            $result['projectData'] = $projectData;
            $result['cardData'] = self::$cardData;
            $cardData = $result['cardData']['data'];
            $cardData =  unique_2d_array_by_key($cardData,"name");
            $result['cardData'] = $cardData;
            $result['productData'] = self::$productData;
            $productData = $result['productData']['data'];
            $productData =  unique_2d_array_by_key($productData,"name");
            $result['productData'] = $productData;
            $this->result('查询成功', $result, 200);
        }
        $this->result('无效的请求', '', 204);

    }


    /**
     * 技师个人业绩
     */
    public function engineerStatistics(){
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if(!isset($getData['time'])|| empty($getData['type']) || empty($getData['engineer_id'])){
                $this->result('缺少时间类型', '', 204);
            }
            $userInfo = $this->userInfo;
            if($getData['type'] == '1') {
                $field = 'created_time';
            }else{
                $field = 'updated_time';
            }
            //下单时间
            if ($getData['time'] == '1') {
                //今天
                $time = 'today';
            }
            if ($getData['time'] == '2') {
                //昨天
                $time = 'yesterday';
            }
            if ($getData['time'] == '3') {
                //本月
                $time = 'month';
            }
            if ($getData['time'] == '4') {
                //上月
                $time = 'last month';
            }
            $data = Db::name('qt_commission_log')
                ->where(['store_id'=>$userInfo['store_id'],'user_id'=>$getData['engineer_id']])
                ->whereTime($field, $time)
                ->select();
            //处理数据
            foreach ($data as $key=>$value){
                //项目
                if($value['type'] == '1'){
                    self::$projectMan += 1;
                    self::$projectMoney += $value['money'];
                    self::$projectCommission += $value['receiver'];
                    if($value['status'] == '1' || $value['status'] == '4' && $value['is_add'] == '1'){
                        self::$projectRow += 1;
                    }
                    if($value['status'] == '2' && $value['is_add'] == '1'){
                        self::$projectClock += 1;
                    }
                    if($value['status'] == '3' && $value['is_add'] == '1'){
                        self::$projectCall += 1;
                    }
                    if( $value['is_add'] == '2'){
                        self::$projectAdd += 1;
                    }
                    if($value['status'] == '1' || $value['status'] == '4' && $value['is_add'] == '2'){
                        self::$projectAddRow += 1;
                    }
                    if($value['status'] == '2'  && $value['is_add'] == '2'){
                        self::$projectAddClock += 1;
                    }
                    self::$projectNumber += 1;
                    self::$projectData[] = $value;
                }
                if($value['type'] == '6'){
                    self::$projectOther += 1;
                }
                //商品
                if($value['type'] == '2'){
                    self::$productMoney += $value['money'];
                    self::$productCommission += $value['receiver'];
                    $goodsNum = Db::name('qt_product_order_item')->where(['id'=>$value['order_project_id']])->value('goods_num');
                    self::$productNumber += $goodsNum;
                    self::$productData[] = $value;
                }
                //售卡
                if($value['is_first'] == '1'){
                    self::$cardMoney += $value['money'];
                    self::$cardCommission += $value['receiver'];
                    self::$cardNumber += 1;
                    self::$cardData[] = $value;
                }
            }
            $userInfo['id'] = $getData['engineer_id'];
            self::$projectData = $this->getengineerData(self::$projectData,'1',$userInfo);
            self::$productData = $this->getengineerData(self::$productData,'2',$userInfo);
            self::$cardData = $this->getengineerData(self::$cardData,'3',$userInfo);
            //用户数量
            self::$user_number =  Db::name('qt_commission_log')
                ->where(['store_id'=>$userInfo['store_id'],'user_id'=>$getData['engineer_id']])
                ->whereTime('created_time', $time)
                ->group('room_id')
                ->count();
            //房间数量
            self::$room_number = Db::name('qt_commission_log')
                ->where(['store_id'=>$userInfo['store_id'],'user_id'=>$getData['engineer_id']])
                ->whereTime('created_time', $time)
                ->group('room_id')
                ->count();
            //客单价
            $total = self::$projectMoney + self::$productMoney;
            if(self::$user_number == '0'){
                self::$averagePrice = '0';
            }else{
                self::$averagePrice = number_format(($total / self::$user_number),'2');
            }
            $result['projectMoney'] = self::$projectMoney;
            $result['projectCommission'] = self::$projectCommission;
            $result['projectNumber'] = self::$projectNumber;
            $result['projectRow'] = self::$projectRow;
            $result['projectClock'] = self::$projectClock;
            $result['projectCall'] = self::$projectCall;
            $result['projectAdd'] = self::$projectAdd;
            $result['projectAddRow'] = self::$projectAddRow;
            $result['projectAddClock'] = self::$projectAddClock;
            $result['projectMan'] = self::$projectMan;
            $result['projectOther'] = self::$projectOther;

            $result['cardMoney'] = self::$cardMoney;
            $result['cardCommission'] = self::$cardCommission;
            $result['cardNumber'] = self::$cardNumber;
            $result['productMoney'] = self::$productMoney;
            $result['productCommission'] = self::$productCommission;
            $result['productNumber'] = self::$productNumber;
            $result['room_number'] = self::$room_number;
            $result['user_number'] = self::$user_number;
            $result['averagePrice'] = self::$averagePrice;
            $result['projectData'] = self::$projectData;
            $projectData = $result['projectData']['data'];
            $projectData =  unique_2d_array_by_key($projectData,"name");
            foreach ($projectData as $key=>$value){
                $projectData[$key]['row'] = self::$projectData['row'];
                $projectData[$key]['row_commission'] = self::$projectData['row_commission'];
                $projectData[$key]['clock'] = self::$projectData['clock'];
                $projectData[$key]['clock_commission'] = self::$projectData['clock_commission'];
                $projectData[$key]['call'] = self::$projectData['call'];
                $projectData[$key]['call_commission'] = self::$projectData['call_commission'];
                $projectData[$key]['add'] = self::$projectData['add'];
                $projectData[$key]['add_commission'] = self::$projectData['add_commission'];
                $projectData[$key]['add_row'] = self::$projectData['add_row'];
                $projectData[$key]['add_row_commission'] = self::$projectData['add_row_commission'];
                $projectData[$key]['add_clock'] = self::$projectData['add_clock'];
                $projectData[$key]['add_clock_commission'] = self::$projectData['add_clock_commission'];
            }
            $result['projectData'] = $projectData;
            $result['cardData'] = self::$cardData;
            $cardData = $result['cardData']['data'];
            $cardData =  unique_2d_array_by_key($cardData,"name");
            $result['cardData'] = $cardData;
            $result['productData'] = self::$productData;
            $productData = $result['productData']['data'];
            $productData =  unique_2d_array_by_key($productData,"name");
            $result['productData'] = $productData;
            $this->result('查询成功', $result, 200);
        }
        $this->result('无效的请求', '', 204);

    }

    /**
     * 获取用户服务详细提成
     * @param $dataName
     * @param $type
     * @param $userInfo
     * @return array
     */
    public function getEngineerData($dataName,$type,$userInfo){
        $row = 0;//轮排
        $row_commission = 0;//轮钟提成
        $clock= 0;//点钟
        $clock_commission= 0;//点钟提成
        $call = 0;//call钟
        $call_commission = 0;//call钟提成
        $add = 0;//加钟
        $add_commission = 0;//加钟提成
        $add_row = 0;//加班轮钟
        $add_row_commission = 0;//加班轮钟提成
        $add_clock = 0;//加班点钟
        $add_clock_commission = 0;//加班点钟提成
        $project_type = '';
        $data['data'] = [];
        //列出服务项目
        foreach ($dataName as $key=>$value){
            if($type == '1'){
                $name = Db::name('qt_project')->where('id',$value['product_id'])->value('name');
                $project_type = Db::name('qt_project')->where('id',$value['product_id'])->value('type');
            }
            if($type == '2'){
                $name = Db::name('qt_goods')->where('id',$value['product_id'])->value('name');
            }
            if($type == '3'){
                $viperTypeName = Db::name('qt_viper_card_recharge_log')
                    ->alias('log')
                    ->leftJoin('qt_viper_card card','card.id = log.viper_card_id')
                    ->leftJoin('qt_viper_type type','type.id = card.viper_type_id')
                    ->where(['log.id'=>$value['order_project_id']])
                    ->value('type.name');
                $name = $viperTypeName;
                $count = Db::name('qt_commission_log')
                    ->where(['store_id'=>$userInfo['store_id'],'user_id'=>$userInfo['id'],'product_id'=>$value['product_id']])
                    ->whereIn('type',['4','5','7'])
                    ->count();
                $money = Db::name('qt_commission_log')
                    ->where(['store_id'=>$userInfo['store_id'],'user_id'=>$userInfo['id'],'product_id'=>$value['product_id']])
                    ->whereIn('type',['4','5','7'])
                    ->sum('money');
                $commission = Db::name('qt_commission_log')
                    ->where(['store_id'=>$userInfo['store_id'],'user_id'=>$userInfo['id'],'product_id'=>$value['product_id']])
                    ->whereIn('type',['4','5','7'])
                    ->sum('commission');
            }else{
                $count = Db::name('qt_commission_log')
                    ->where(['store_id'=>$userInfo['store_id'],'user_id'=>$userInfo['id'],'type'=>$type,'product_id'=>$value['product_id']])
                    ->count();
                $money = Db::name('qt_commission_log')
                    ->where(['store_id'=>$userInfo['store_id'],'user_id'=>$userInfo['id'],'type'=>$type,'product_id'=>$value['product_id']])
                    ->sum('money');
                $commission = Db::name('qt_commission_log')
                    ->where(['store_id'=>$userInfo['store_id'],'user_id'=>$userInfo['id'],'type'=>$type,'product_id'=>$value['product_id']])
                    ->sum('commission');
            }

            $data['data'][$key]['name'] = $name;
            $data['data'][$key]['number'] = $count;
            $data['data'][$key]['money'] = $money;
            $data['data'][$key]['commission'] = $commission;
            $data['data'][$key]['project_type'] = $project_type;
            if($value['status'] == '1'  || $value['status'] == '4'){
                $row += 1;
                $row_commission += $value['commission'];;
            }
            //排加班
            if($value['status'] == '1'  || $value['status'] == '4' && $value['is_add'] == '2'){
                $add_row += 1;
                $add_row_commission += $value['commission'];
            }
            //点加班
            if($value['status'] == '2'  && $value['is_add'] == '2'){
                $add_clock += 1;
                $add_clock_commission += $value['commission'];
            }

            if($value['status'] == '2' ){
                $clock += 1;
                $clock_commission += $value['commission'];
            }
            if($value['status'] == '3' ){
                $call += 1;
                $call_commission += $value['commission'];
            }
            if($value['status'] == '5' ){
                $add += 1;
                $add_commission += $value['commission'];
            }
        }
        $data['row'] = $row;
        $data['row_commission'] = $row_commission;
        $data['clock'] = $clock;
        $data['clock_commission'] = $clock_commission;
        $data['call'] = $call;
        $data['call_commission'] = $call_commission;
        $data['add'] = $add;
        $data['add_commission'] = $add_commission;
        $data['add_row'] = $add_row;
        $data['add_row_commission'] = $add_row_commission;
        $data['add_clock'] = $add_clock;
        $data['add_clock_commission'] = $add_clock_commission;
        return $data;
    }



    /**
     * 获取用户服务详细提成
     * @param $dataName
     * @param $type
     * @param $userInfo
     * @return array
     */
    public function getData($dataName,$type,$userInfo){
        $row = 0;//轮排
        $clock= 0;//点钟
        $call = 0;//call钟
        $add = 0;//加钟
        $project_type = '';
        $data['data'] = [];
        //列出服务项目
        foreach ($dataName as $key=>$value){
            if($type == '1'){
                $name = Db::name('qt_project')->where('id',$value['product_id'])->value('name');
                $project_type = Db::name('qt_project')->where('id',$value['product_id'])->value('type');
            }
            if($type == '2'){
                $name = Db::name('qt_goods')->where('id',$value['product_id'])->value('name');
            }
            if($type == '3'){
                $viperTypeName = Db::name('qt_viper_card_recharge_log')
                    ->alias('log')
                    ->leftJoin('qt_viper_card card','card.id = log.viper_card_id')
                    ->leftJoin('qt_viper_type type','type.id = card.viper_type_id')
                    ->where(['log.id'=>$value['order_project_id']])
                    ->value('type.name');
                $name = $viperTypeName;
                $count = Db::name('qt_commission_log')
                    ->where(['store_id'=>$userInfo['store_id'],'cashier_id'=>$userInfo['id'],'product_id'=>$value['product_id']])
                    ->whereIn('type',['4','5','7'])
                    ->count();
                $money = Db::name('qt_commission_log')
                    ->where(['store_id'=>$userInfo['store_id'],'cashier_id'=>$userInfo['id'],'product_id'=>$value['product_id']])
                    ->whereIn('type',['4','5','7'])
                    ->sum('money');
                $commission = Db::name('qt_commission_log')
                    ->where(['store_id'=>$userInfo['store_id'],'cashier_id'=>$userInfo['id'],'product_id'=>$value['product_id']])
                    ->whereIn('type',['4','5','7'])
                    ->sum('receiver');
            }else{
                if($value['type'] == '2'){
                    $count = Db::name('qt_commission_log')
                        ->alias('log')
                        ->leftJoin('qt_product_order_item item','item.id = log.order_project_id')
                        ->where(['log.store_id'=>$userInfo['store_id'],'log.cashier_id'=>$userInfo['id'],'log.type'=>$type,'log.product_id'=>$value['product_id']])
                        ->sum('item.goods_num');
                }else{
                    $count = Db::name('qt_commission_log')
                        ->where(['store_id'=>$userInfo['store_id'],'cashier_id'=>$userInfo['id'],'type'=>$type,'product_id'=>$value['product_id']])
                        ->count();
                }
                $money = Db::name('qt_commission_log')
                    ->where(['store_id'=>$userInfo['store_id'],'cashier_id'=>$userInfo['id'],'type'=>$type,'product_id'=>$value['product_id']])
                    ->sum('money');
                $commission = Db::name('qt_commission_log')
                    ->where(['store_id'=>$userInfo['store_id'],'cashier_id'=>$userInfo['id'],'type'=>$type,'product_id'=>$value['product_id']])
                    ->sum('receiver');
            }

            $data['data'][$key]['name'] = $name;
            $data['data'][$key]['number'] = $count;
            $data['data'][$key]['money'] = $money;
            $data['data'][$key]['commission'] = $commission;
            $data['data'][$key]['project_type'] = $project_type;
            $data['data'][$key]['row'] = 0;
            $data['data'][$key]['clock'] = 0;
            $data['data'][$key]['add'] = 0;
            $data['data'][$key]['call'] = 0;
            if($value['status'] == '1'  || $value['status'] == '4'){
                $row += 1;
                $data['data'][$key]['row'] += 1;
            }
            if($value['status'] == '2' ){
                $clock += 1;
                $data['data'][$key]['clock'] += 1;
            }
            if($value['status'] == '3' ){
                $call += 1;
                $data['data'][$key]['call'] += 1;
            }
            if($value['status'] == '5' ){
                $add += 1;
                $data['data'][$key]['add'] += 1;
            }
        }
        $data['row'] = $row;
        $data['clock'] = $clock;
        $data['call'] = $call;
        $data['add'] = $add;
        return $data;
    }

    /**
     * 楼牌统计技师
     */
    public function storeUser(){
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if(!isset($getData['type'])|| empty($getData['type']) ){
                $this->result('缺少时间类型', '', 204);
            }
            $userInfo = $this->userInfo;
            if($getData['type'] == '1') {
                $field = 'created_time';
            }else{
                $field = 'updated_time';
            }
//            //下单时间
//            if ($getData['time'] == '1') {
//                //今天
//                $time = 'today';
//            }
//            if ($getData['time'] == '2') {
//                //昨天
//                $time = 'yesterday';
//            }
//            if ($getData['time'] == '3') {
//                //本月
//                $time = 'month';
//            }
//            if ($getData['time'] == '4') {
//                //上月
//                $time = 'last month';
//            }

            $startTime = 0;
            $endTime = 0;
            if(isset($getData['start_time'])){
                $startTime = strtotime($getData['start_time']);
            }
            if(isset($getData['end_time'])){
                $endTime = strtotime($getData['end_time']);
            }
            //用户数据
            $query = Db::name('qt_commission_log')
                ->field('cl.id,cl.user_id,u.work_number')
                ->alias('cl')
                ->leftJoin('qt_users u','u.id = cl.user_id')
                ->where(['cl.store_id'=>$userInfo['store_id']])
                ->where('cl.user_id','neq','0');
            if (!empty($startTime) && !empty($endTime)) {
                $query->whereBetweenTime('cl.'.$field,$startTime,$endTime);
            }

            $userRes = $query->group('cl.user_id')
                ->select();
            //总数据
            $dataRes = Db::name('qt_commission_log')
                ->where(['store_id'=>$userInfo['store_id']])
                ->whereBetweenTime($field,$startTime,$endTime)
                ->where('user_id','neq','0')
                ->select();
            //构造返回数据
            static $result = [];
            foreach ($userRes as $key=>$value){
                $result['data'][$value['user_id']]['user_id'] = $value['user_id'];
                $result['data'][$value['user_id']]['project'] = 0;
                $result['data'][$value['user_id']]['product'] = 0;
                $result['data'][$value['user_id']]['money'] = 0;
                $result['data'][$value['user_id']]['card'] = 0;
                $result['data'][$value['user_id']]['commission'] = 0;
                $result['data'][$value['user_id']]['work_number'] = $value['work_number'];
                $result['total']['project'] = 0;
                $result['total']['product'] = 0;
                $result['total']['money'] = 0;
                $result['total']['card'] = 0;
                $result['total']['commission'] = 0;

            }
            //数据填充
            //订单类型 1 轮排 2  点钟 3 call钟  4 优轮 5 加班
            foreach ($dataRes as $key=>$value){
                if($value['type'] == '1'){
                    $result['data'][$value['user_id']]['project'] += 1;
                    $result['total']['project']  += 1;
                }
                if($value['type'] == '2'){
                    $goodsNum = Db::name('qt_product_order_item')->where(['id'=>$value['order_project_id']])->value('goods_num');
                    $result['data'][$value['user_id']] ['product']+= $goodsNum;
                    $result['total']['product']  += 1;
                }
                if($value['is_first'] == '1'){
                    $result['data'][$value['user_id']]['card'] += 1;
                    $result['total']['card']  += 1;
                }
                $result['data'][$value['user_id']]['money'] += $value['money'];
                $result['total']['money']  += $value['money'];
                $result['data'][$value['user_id']]['commission'] += $value['commission'];
                $result['total']['commission']  += $value['commission'];
            }
            if(empty($result)){
                $result = [];
            }else{
                $result['data'] = array_values($result['data']);
            }
            $this->result('查询成功', $result, 200);
        }
    }


    /**
     * 楼牌统计技师
     */
    public function storeUserWork(){
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if(empty($getData['type']) ){
                $this->result('缺少时间类型', '', 204);
            }
            $userInfo = $this->userInfo;
            $startTime = $endTime = 0;
            if(isset($getData['start_time'])){
                $startTime = strtotime($getData['start_time']);
            }
            if(isset($getData['end_time'])){
                $endTime = strtotime($getData['end_time']);
            }

            //用户数据
            $query = Db::name('qt_commission_log')
                ->alias('cl')
                ->field('cl.id,cl.created_time,cl.updated_time,cl.user_id,u.real_name,u.work_number,cl.status')
                ->leftJoin('qt_users u','u.id = cl.user_id')
                ->where(['cl.store_id'=>$userInfo['store_id']])
                ->where('cl.user_id','neq',0);
            if (!empty($startTime) && !empty($endTime)) {
                $query->whereBetweenTime('cl.created_time',$startTime,$endTime);
            }
            $result = [];
            $result['data'] = [];
            $data['data'] = [];
            $userRes = $query->group('cl.user_id')->select();
            if(!empty($userRes)){
                $result['total']['row'] = 0;
                $result['total']['clock'] = 0;
                $result['total']['call'] = 0;
                $result['total']['add'] = 0;
                $result['total']['add_work'] = 0;
                $result['total']['product'] = 0;
                $result['total']['main_item'] = 0;
                $result['total']['adjunct'] = 0;
                foreach ($userRes as $key => $value){
                    $query = Db::name('qt_commission_log')
                        ->field('cl.*,u.real_name,u.work_number')
                        ->alias('cl')
                        ->leftJoin('qt_users u','u.id = cl.user_id');
                    if (!empty($startTime) && !empty($endTime)) {
                        $query->whereBetweenTime('cl.created_time',$startTime,$endTime);
                    }
                    $dataRes = Db::name('qt_commission_log')
                        ->alias('cl')
                        ->field('cl.id,cl.created_time,cl.updated_time,cl.user_id,u.real_name,u.work_number,cl.status,p.type,cl.type as order_type')
                        ->leftJoin('qt_users u','u.id = cl.user_id')
                        ->leftJoin('qt_project p ','p.id = cl.product_id')
                        ->whereBetweenTime('cl.created_time',$startTime,$endTime)
                        ->where(['cl.store_id'=>$userInfo['store_id'],'cl.user_id'=>$value['user_id']])
                        ->select();
                    foreach ($dataRes as $k => $val) {
                        $result['data'][$val['user_id']]['user_id'] = $val['user_id'];
                        $result['data'][$val['user_id']]['row'] = 0;
                        $result['data'][$val['user_id']]['clock'] = 0;
                        $result['data'][$val['user_id']]['call'] = 0;
                        $result['data'][$val['user_id']]['card'] = 0;
                        $result['data'][$val['user_id']]['add'] = 0;
                        $result['data'][$val['user_id']]['add_work'] = 0;
                        $result['data'][$val['user_id']]['product'] = 0;
                        $result['data'][$val['user_id']]['main_item'] = 0;
                        $result['data'][$val['user_id']]['adjunct'] = 0;
                        $result['data'][$val['user_id']]['work_number'] = $val['work_number'];
                    }


                    //订单类型 1 轮排 2  点钟 3 call钟  4 优轮 5 加钟 6 加班
                    foreach ($dataRes as $d_key => $d_value){
                        if($d_value['status'] == '1' || $d_value['status'] == '4'){
                            $result['data'][$d_value['user_id']]['row'] += 1;
                            $result['total']['row']  += 1;
                        }
                        if($d_value['status'] == '2'){
                            $result['data'][$d_value['user_id']] ['clock']+= 1;
                            $result['total']['clock']  += 1;
                        }
                        if($d_value['status'] == '3'){
                            $result['data'][$d_value['user_id']]['call'] += 1;
                            $result['total']['call']  += 1;
                        }
                        if($d_value['status'] == '5'){
                            $result['data'][$d_value['user_id']]['add'] += 1;
                            $result['total']['add']  += 1;
                        }
                        if($d_value['status'] == '6'){
                            $result['data'][$d_value['user_id']]['add_word'] += 1;
                            $result['total']['add_word']  += 1;
                        }

                        if ($d_value['order_type'] == 1) {
                            $result['data'][$d_value['user_id']]['main_item'] += 1;
                            $result['total']['main_item']  += 1;
                        }

                        if ($d_value['order_type'] == 6) {
                            $result['data'][$d_value['user_id']]['adjunct'] += 1;
                            $result['total']['adjunct']  += 1;
                        }
                    }
                }
                $data = $result;
            }else{
                $data = [];
            }
            if(isset($data['data'])){
                $data['data'] = array_values($data['data']);
            }
            $this->result('查询成功', $data, 200);
        }else {

            $this->result('请求异常', '', 204);
        }
    }


    /**
     * 楼牌统计技师
     */
    public function reception(){
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if(!isset($getData['type'])|| empty($getData['type']) ){
                $this->result('缺少时间类型', '', 204);
            }
            $userInfo = $this->userInfo;
            if($getData['type'] == '1') {
                $field = 'created_time';
            }else{
                $field = 'updated_time';
            }
            //下单时间
            if ($getData['time'] == '1') {
                //今天
                $time = 'today';
            }
            if ($getData['time'] == '2') {
                //昨天
                $time = 'yesterday';
            }
            if ($getData['time'] == '3') {
                //本月
                $time = 'month';
            }
            if ($getData['time'] == '4') {
                //上月
                $time = 'last month';
            }
            $startTime = $endTime =0;
            if(isset($getData['start_time'])){
                $startTime = strtotime($getData['start_time']);
            }
            if(isset($getData['end_time'])){
                $endTime = strtotime($getData['end_time']);
            }
            //用户数据
            $query = Db::name('qt_commission_log')
                ->field('cl.id,cl.cashier_id,u.real_name')
                ->alias('cl')
                ->leftJoin('qt_users u','u.id = cl.cashier_id')
                ->where(['cl.store_id'=>$userInfo['store_id'],'u.group'=>'2'])
                ->whereTime('cl.'.$field, $time);
                if (!empty($startTime) && !empty($endTime)) {
                    $query->whereBetweenTime('cl.'.$field,$startTime,$endTime);
                }
               $userRes =  $query->group('cl.cashier_id')
                ->select();
            //总数据
            $dataRes = Db::name('qt_commission_log')
                ->field('cl.*')
                ->alias('cl')
                ->leftJoin('qt_users u','u.id = cl.cashier_id')
                ->where(['cl.store_id'=>$userInfo['store_id'],'u.group'=>'2'])
                ->whereBetweenTime('cl.'.$field,$startTime,$endTime)
                ->whereTime('cl.'.$field, $time)
                ->select();
            //构造返回数据
            static $result = [];
            foreach ($userRes as $key=>$value){
                $result['data'][$value['cashier_id']]['user_id'] = $value['cashier_id'];
                $result['data'][$value['cashier_id']]['project'] = 0;
                $result['data'][$value['cashier_id']]['product'] = 0;
                $result['data'][$value['cashier_id']]['money'] = 0;
                $result['data'][$value['cashier_id']]['payment_money'] = 0;
                $result['data'][$value['cashier_id']]['card'] = 0;
                $result['data'][$value['cashier_id']]['commission'] = 0;
                $result['data'][$value['cashier_id']]['real_name'] = $value['real_name'];
                $result['total']['project'] = 0;
                $result['total']['product'] = 0;
                $result['total']['money'] = 0;
                $result['total']['card'] = 0;
                $result['total']['commission'] = 0;
            }
            //数据填充
            //订单类型 1 轮排 2  点钟 3 call钟  4 优轮 5 加班
            foreach ($dataRes as $key=>$value){
                if($value['type'] == '1'){
                    $result['data'][$value['cashier_id']]['project'] += 1;
                    $result['total']['project']  += 1;
                }
                if($value['type'] == '2'){
                    $goodsNumber = Db::name('qt_product_order_item')->where(['id'=>$value['order_project_id']])->value('goods_num');
                    $result['data'][$value['cashier_id']] ['product']+= $goodsNumber;
                    $result['total']['product']  += $goodsNumber;
                }
                if($value['is_first'] == '1'){
                    $result['data'][$value['cashier_id']]['card'] += 1;
                    $result['total']['card']  += 1;
                }
                $result['data'][$value['cashier_id']]['money'] += $value['money'];
                $result['total']['money']  += $value['money'];
                $result['data'][$value['cashier_id']]['commission'] += $value['receiver'];
                if($value['type'] == '3' ||$value['type'] == '4' ||$value['type'] == '5'||$value['type'] == '7'){
                    $result['data'][$value['cashier_id']]['payment_money'] = $value['money'];
                }else{
                    $result['data'][$value['cashier_id']]['payment_money'] = $value['payment_price'];
                }
            }
            if(empty($result)){
                $this->result('查询成功', $result, 200);
            }
            $result['data'] = array_values($result['data']);
            $this->result('查询成功', $result, 200);
        }
    }


    /**
     * 门店业绩
     */
    public function store(){
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if(!isset($getData['time'])|| empty($getData['time']) ){
                $this->result('缺少时间类型', '', 204);
            }
            $userInfo = $this->userInfo;
            //下单时间
            if ($getData['time'] == '1') {
                //今天
                $time = 'today';
            }
            if ($getData['time'] == '2') {
                //昨天
                $time = 'yesterday';
            }
            if ($getData['time'] == '3') {
                //本月
                $time = 'month';
            }
            if ($getData['time'] == '4') {
                //上月
                $time = 'last month';
            }
            //总金额
            $result['totalPrice'] = Db::name('qt_commission_log')
                ->where('store_id',$userInfo['store_id'])
                ->whereNull('is_first')
                ->whereTime('created_time', $time)
                ->sum('money');
            //现金（现金加微信加支付宝等）
            $result['totalCash'] = Db::name('qt_check_out_pay')
                ->where(['store_id'=>$userInfo['store_id'],'type'=>'2'])
                ->whereTime('create_time', $time)
                ->sum('pay_money');
            //其他收入
            $result['totalOther']  = Db::name('qt_check_out_pay')
                ->where(['type'=>'2'])
                ->whereIn('pay_id',['5','6'])
                ->where('store_id',$userInfo['store_id'])
                ->whereTime('create_time', $time)
                ->sum('pay_money');
            //现金消费(现金)
            $result['totalCashConsumption']  = Db::name('qt_check_out_pay')
                ->where(['type'=>'2','pay_id'=>'1'])
                ->where('store_id',$userInfo['store_id'])
                ->whereTime('create_time', $time)
                ->sum('pay_money');
            //售卡业绩
            $result['totalCard'] =  Db::name('qt_commission_log')
                ->where('is_first','1')
                ->where('store_id',$userInfo['store_id'])
                ->whereTime('created_time', $time)
                ->sum('money');
            //新增会员
            $result['totalAddUser'] = Db::name('qt_viper_card')
                ->where('store_id',$userInfo['store_id'])
                ->whereTime('create_time', $time)
                ->count();
            //商品业绩
            $result['totalProduct'] =  Db::name('qt_commission_log')
                ->alias('log')
                ->leftJoin('qt_product_order_item item','log.order_project_id = item.id')
                ->where('log.type','2')
                ->where('log.store_id',$userInfo['store_id'])
                ->whereTime('log.created_time', $time)
                ->sum('item.goods_total_price');
            //消费人数
            $result['totalUser'] =  Db::name('qt_check_out_item')
                ->alias('item')
                ->leftJoin('qt_check_out out','out.id = item.check_out_id')
                ->where('out.store_id',$userInfo['store_id'])
                ->whereTime('item.create_time', $time)
                ->group('item.check_out_id')
                ->count();
            if($result['totalUser'] == '0' || $result['totalPrice'] == '0'){
                $result['totalUserAverage'] = 0;
            }else{
                //人均
                $result['totalUserAverage']  = number_format(($result['totalPrice'] / $result['totalUser']),0);
            }
            //会员消费数
            $result['totalUserConsumption'] =  Db::name('qt_check_out_pay')
                ->where(['store_id'=>$userInfo['store_id'],'type'=>'2'])
                ->whereTime('create_time', $time)
                ->group('viper_card_id')
                ->count();

            //会员续费
            $result['totalUserRenew'] =  Db::name('qt_commission_log')
                ->where('store_id',$userInfo['store_id'])
                ->where('is_first','2')
                ->whereTime('created_time', $time)
                ->group('card_id')
                ->count();
            //赠送金额
            $result['totalGiveMoney'] =  Db::name('qt_commission_log')
                ->alias('cl')
                ->leftJoin('qt_user_viper uv','uv.id = cl.user_id')
                ->where('cl.store_id',$userInfo['store_id'])
                ->where('cl.type','in',['3','4'])
                ->whereTime('cl.created_time', $time)
                ->sum('give_money');

            //项目提成
            $result['projectCommission'] =  Db::name('qt_commission_log')
                ->where('store_id',$userInfo['store_id'])
                ->where('type','1')
                ->whereTime('updated_time', $time)
                ->sum('commission');

            //项目业绩
            $result['projectAchievement'] =  Db::name('qt_commission_log')
                ->where('store_id',$userInfo['store_id'])
                ->where('type','1')
                ->whereTime('created_time', $time)
                ->sum('money');
            //接待客服
            $result['roomCount'] =  Db::name('qt_commission_log')
                ->where('store_id',$userInfo['store_id'])
                ->whereTime('created_time', $time)
                ->group('room_id')
                ->count();
            //售卡提成
            $result['cardCommission'] =  Db::name('qt_commission_log')
                ->where('store_id',$userInfo['store_id'])
                ->where('is_first','1')
                ->whereTime('created_time', $time)
                ->sum('commission');
            $this->result('查询成功', $result, 200);
        }
    }

    /**
     * 楼面提成明细
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function receptionDetail(){
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if(!isset($getData['type'])|| empty($getData['type']) ){
                $this->result('缺少时间类型', '', 204);
            }
            if($getData['type'] == '1') {
                $field = 'log.created_time';
            }else{
                $field = 'log.updated_time';
            }
            $startTime = strtotime($getData['start_time']);
            $endTime = strtotime($getData['end_time']);
            $projectData = Db::name('qt_commission_log')
                ->field('p.name as project_name,p.price,p.payment_price,p.created_time,log.receiver,r.name as room_name')
                ->alias('log')
                ->leftJoin('qt_order_project p','log.order_project_id = p.id')
                ->leftJoin('qt_room r','r.id = p.room_id')
                ->where(['cashier_id'=>$getData['user_id'],'type'=>'1'])
                ->whereBetweenTime($field,$startTime,$endTime)
                ->select();
            $goodsData = Db::name('qt_commission_log')
                ->field('item.goods_name as project_name,item.goods_total_price as price,item.payment_price,item.create_time as created_time,log.receiver,r.name as room_name')
                ->alias('log')
                ->leftJoin('qt_product_order_item item','log.order_project_id = item.id')
                ->leftJoin('qt_room r','r.id = item.room_id')
                ->where(['cashier_id'=>$getData['user_id'],'type'=>'2'])
                ->whereBetweenTime($field,$startTime,$endTime)
                ->select();
            $result['project'] = array_merge($projectData,$goodsData);
            //会员卡
            $cardData = Db::name('qt_commission_log')
                ->field('card.name as card_name,recharge.amount,recharge.give_amount,log.created_time,card.card_no,log.receiver')
                ->alias('log')
                ->leftJoin('qt_viper_card_recharge_log recharge','log.order_project_id = recharge.id')
                ->leftJoin('qt_viper_card card','card.id = recharge.viper_card_id')
                ->where(['cashier_id'=>$getData['user_id']])
                ->whereIn('log.type',[3,4,5,7])
                ->whereBetweenTime($field,$startTime,$endTime)
                ->select();
            $result['card'] = $cardData;
            $this->result('查询成功', $result, 200);
        }
    }


    /**
     * 技师提成明细项目明细
     */
    public function engineerStatisticsDetail(){
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if(!isset($getData['type'])|| empty($getData['type']) ){
                $this->result('缺少时间类型', '', 204);
            }
            if($getData['type'] == '1') {
                $field = 'log.created_time';
            }else{
                $field = 'log.updated_time';
            }
            $startTime = strtotime($getData['start_time']);
            $endTime = strtotime($getData['end_time']);
            $projectData = Db::name('qt_commission_log')
                ->field('p.name as project_name,p.price,p.payment_price,p.created_time,log.commission,r.name as room_name')
                ->alias('log')
                ->leftJoin('qt_order_project p','log.order_project_id = p.id')
                ->leftJoin('qt_room r','r.id = p.room_id')
                ->where(['user_id'=>$getData['user_id'],'type'=>'1'])
                ->whereBetweenTime($field,$startTime,$endTime)
                ->select();
            $goodsData = Db::name('qt_commission_log')
                ->field('item.goods_name as project_name,item.goods_total_price as price,item.payment_price,item.create_time as created_time,log.commission,r.name as room_name')
                ->alias('log')
                ->leftJoin('qt_product_order_item item','log.order_project_id = item.id')
                ->leftJoin('qt_room r','r.id = item.room_id')
                ->where(['user_id'=>$getData['user_id'],'type'=>'2'])
                ->whereBetweenTime($field,$startTime,$endTime)
                ->select();
            $result['project'] = array_merge($projectData,$goodsData);
            //会员卡
            $cardData = Db::name('qt_commission_log')
                ->field('card.name as card_name,recharge.amount,recharge.give_amount,log.created_time,card.card_no,log.commission')
                ->alias('log')
                ->leftJoin('qt_viper_card_recharge_log recharge','log.order_project_id = recharge.id')
                ->leftJoin('qt_viper_card card','card.id = recharge.viper_card_id')
                ->where(['user_id'=>$getData['user_id']])
                ->whereIn('log.type',[3,4,5,7])
                ->whereBetweenTime($field,$startTime,$endTime)
                ->select();
            $result['card'] = $cardData;
            $this->result('查询成功', $result, 200);
        }
    }


    /**
     * 统计图表
     */
    public function storeChart(){
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if(!isset($getData['time'])|| empty($getData['time']) ){
                $this->result('缺少时间类型', '', 204);
            }
            $userInfo = $this->userInfo;
            //下单时间
            if ($getData['time'] == '1') {
                //今天
                $time = 'today';
            }
            if ($getData['time'] == '2') {
                //昨天
                $time = 'yesterday';
            }
            //下单时间
            if ($getData['time'] == '3') {
                //本月
                $time = 'month';
            }
            if ($getData['time'] == '4') {
                //上月
                $time = 'last month';
            }
            //总金额
            $result['total_money'] = $this->getMoneyTotal($userInfo['store_id'],$time,'3');
            $result['total'] = $this->getMoneyTotal($userInfo['store_id'],$time,'1');
            /****************** 其他收入 ***********************/
            $result['other'] = $this->getMoneyTotal($userInfo['store_id'],$time,'2');
            /****************** 柱状图 ***********************/
            $result['histogram'] = $this->histogram($result['total_money']['series']['data']);
            /****************** 会员充值 ***********************/
            $result['userRecharge'] = $this->getCardTotal($userInfo['store_id'],$time,'1');
            /****************** 会员消费 ***********************/
            $result['userConsumption'] = $this->getCardTotal($userInfo['store_id'],$time,'2');
            $this->result('查询成功', $result, 200);
        }
    }


    /**
     * 获取消费数据统计
     * @param $storeId[门店ID]
     * @param $time[时间类型] [1 今天 2 昨天 3 本月 4 上月]
     * @param $type [类型] [1营收 2 非营收]
     * //pay_id  1,现金 2,银联 3,微信 4,支付宝 5,消费券 6,会员卡 7,折免
     */
    public function getMoneyTotal($storeId,$time,$type){
        if($type == 1){
            $where['type'] = 2;
        }elseif($type == '2'){
            $where['type'] = [1,3];
        }else{
            $where['type'] = [1,2,3];
        }
        //总金额
        $result['totalPrice'] = Db::name('qt_check_out_pay')
            ->where('store_id',$storeId)
            ->whereTime('create_time', $time)
            ->where($where)
            ->sum('pay_money');
        //现金
        $result['totalCash'] = Db::name('qt_check_out_pay')
            ->where('pay_id',1)
            ->where('store_id',$storeId)
            ->whereTime('create_time', $time)
            ->where($where)
            ->sum('pay_money');
        //微信
        $result['totalWechat']  = Db::name('qt_check_out_pay')
            ->where('pay_id',3)
            ->where('store_id',$storeId)
            ->whereTime('create_time', $time)
            ->where($where)
            ->sum('pay_money');
        //支付宝
        $result['totalAlipay']  = Db::name('qt_check_out_pay')
            ->where('pay_id',4)
            ->where('store_id',$storeId)
            ->whereTime('create_time', $time)
            ->where($where)
            ->sum('pay_money');
        //会员卡
        $result['totalCard']  = Db::name('qt_check_out_pay')
            ->where('pay_id',6)
            ->where('store_id',$storeId)
            ->whereTime('create_time', $time)
            ->where($where)
            ->sum('pay_money');
        //优惠金额
        $result['totalCoupon']  = Db::name('qt_check_out_pay')
            ->where('store_id',$storeId)
            ->where('pay_id',5)
            ->whereTime('create_time', $time)
            ->where($where)
            ->sum('pay_money');
        //银联
        $result['totalUnionPay']  = Db::name('qt_check_out_pay')
            ->where('store_id',$storeId)
            ->where('pay_id',2)
            ->whereTime('create_time', $time)
            ->where($where)
            ->sum('pay_money');
        //折免
        $result['totalFracture']  = Db::name('qt_check_out_pay')
            ->where('store_id',$storeId)
            ->where('pay_id',7)
            ->whereTime('create_time', $time)
            ->where($where)
            ->sum('pay_money');
        $data[0]['name'] = '支付宝';
        $data[0]['value'] = $result['totalAlipay'];
        $data[1]['name'] = '微信';
        $data[1]['value'] =  $result['totalWechat'];
        $data[2]['name'] = '现金';
        $data[2]['value'] =  $result['totalCash'];
        $data[3]['name'] = '会员卡';
        $data[3]['value'] =  $result['totalCard'];
        $data[4]['name'] = '优惠券';
        $data[4]['value'] =  $result['totalCoupon'];
        $data[5]['name'] = '银联';
        $data[5]['value'] =  $result['totalUnionPay'];
        $data[6]['name'] = '折免';
        $data[6]['value'] =  $result['totalFracture'];
        $res['total'] =  $result['totalPrice'];
        $res['series']['data'] = $data;
        return $res;
    }

    /**
     * 获取门店项目详情
     */
    public function storeProject(){

        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if(!isset($getData['time'])|| empty($getData['time']) ){
                $this->result('缺少时间类型', '', 204);
            }
            $userInfo = $this->userInfo;
            //下单时间
            if ($getData['time'] == '1') {
                //今天
                $time = 'today';
            }
            if ($getData['time'] == '2') {
                //昨天
                $time = 'yesterday';
            }
            //下单时间
            if ($getData['time'] == '3') {
                //本月
                $time = 'month';
            }
            if ($getData['time'] == '4') {
                //上月
                $time = 'last month';
            }
            if($getData['type'] == '1'){
                $type = 'cl.created_time';
            }
            if($getData['type'] == '2'){
                $type = 'cl.updated_time';
            }
            $groupRes = Db::name('qt_commission_log')
                ->field('cl.*,p.name')
                ->alias('cl')
                ->leftJoin('qt_project p','cl.product_id = p.id')
                ->where(['cl.store_id'=>$userInfo['store_id'],'cl.type'=>'1'])
                ->whereTime($type,$time)
                ->group('cl.product_id')
                ->select();

            $result = [];
            $result['total']['money'] = 0;
            $result['total']['number'] = 0;

            foreach ($groupRes as $key=>$value){
                $dataRes = Db::name('qt_commission_log')
                    ->field('cl.status,cl.money')
                    ->alias('cl')
                    ->leftJoin('qt_project p','cl.product_id = p.id')
                    ->where(['cl.store_id'=>$userInfo['store_id'],'cl.type'=>'1','cl.product_id'=>$value['product_id']])
                    ->whereTime($type,$time)
                    ->select();
                $result['list'][$key]['name'] = $value['name'];
                $result['list'][$key]['number'] = count($dataRes);
                $result['list'][$key]['row']  = 0;
                $result['list'][$key]['clock']  = 0;
                $result['list'][$key]['call']  = 0;
                $result['list'][$key]['add']  = 0;
                $result['list'][$key]['money']  = 0;

                //总数
                $result['total']['number'] += $result['list'][$key]['number'];

                foreach ($dataRes as $k=>$v){

                    //总金额
                    $result['total']['money'] += $v['money'];

                    $result['list'][$key]['money'] += $v['money'];
                    if($v['status'] == '1' || $v['status'] == '4'){
                        $result['list'][$key]['row'] += 1;
                    }
                    if($v['status'] == '2'){
                        $result['list'][$key]['clock'] += 1;
                    }
                    if($v['status'] == '3'){
                        $result['list'][$key]['call'] += 1;
                    }
                    if($v['status'] == '5'){
                        $result['list'][$key]['add'] += 1;
                    }
                }
            }
            $this->result('查询成功',$result,200);

        }
    }


    /**
     * 店铺商品统计
     */
    public function storeProduct(){
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if(!isset($getData['time'])|| empty($getData['time']) ){
                $this->result('缺少时间类型', '', 204);
            }
            $userInfo = $this->userInfo;
            //下单时间
            if ($getData['time'] == '1') {
                //今天
                $time = 'today';
            }
            if ($getData['time'] == '2') {
                //昨天
                $time = 'yesterday';
            }
            //下单时间
            if ($getData['time'] == '3') {
                //本月
                $time = 'month';
            }
            if ($getData['time'] == '4') {
                //上月
                $time = 'last month';
            }
            if($getData['type'] == '1'){
                $type = 'item.create_time';
            }
            if($getData['type'] == '2'){
                $type = 'item.pay_time';
            }
            $data = Db::name('qt_product_order_item')
                ->field('g.id,g.name,sum(item.goods_num) as total_number,sum(item.goods_total_price) as total_money')
                ->alias('item')
                ->leftJoin('qt_goods g','item.goods_id = g.id')
                ->where(['item.store_id'=>$userInfo['store_id']])
                ->whereTime($type,$time)
                ->group('item.goods_id')
                ->select();
            foreach ($data as $key=>$value){
                $list = Db::name('qt_product_order_item')
                    ->field('p.name,item.goods_price,item.goods_num,item.goods_total_price')
                    ->alias('item')
                    ->leftJoin('qt_goods p','item.goods_id = p.id')
                    ->where(['item.store_id'=>$userInfo['store_id'],'p.id'=>$value['id']])
                    ->whereTime($type,$time)
                    ->select();
                $data[$key]['list'] = $list;
            }

//            dump($data);die;
//
//
//            $result = [];
//            $result['total']['money'] = Db::name('qt_product_order_item')
//                ->alias('item')
//                ->leftJoin('qt_goods p','item.goods_id = p.id')
//                ->where(['item.store_id'=>$userInfo['store_id']])
//                ->whereTime($type,$time)
//                ->sum('item.goods_total_price');
//            $result['total']['number'] = Db::name('qt_product_order_item')
//                ->alias('item')
//                ->leftJoin('qt_goods p','item.goods_id = p.id')
//                ->where(['item.store_id'=>$userInfo['store_id']])
//                ->whereTime($type,$time)
//                ->sum('item.goods_num');
//            $result['list'] = $list;
            $this->result('查询成功',$data,200);

        }
    }

    /**
     * 获取售卡统计
     */
    public function storeCard(){
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if(!isset($getData['time'])|| empty($getData['time']) ){
                $this->result('缺少时间类型', '', 204);
            }
            $limit = !empty($getData['limit']) ? $getData['limit'] : 10;
            $userInfo = $this->userInfo;
            //下单时间
            if ($getData['time'] == '1') {
                //今天
                $time = 'today';
            }
            if ($getData['time'] == '2') {
                //昨天
                $time = 'yesterday';
            }
            //下单时间
            if ($getData['time'] == '3') {
                //本月
                $time = 'month';
            }
            if ($getData['time'] == '4') {
                //上月
                $time = 'last month';
            }

            $result = Db::name('qt_viper_card_recharge_log')->alias('log')
                ->field([
                    'log.sale_id',
                    'u.real_name',
                    'sum(log.commission) as commission',
                    'sum(log.amount) as amount',
                ])
                ->where([
                    'log.store_id'=>$userInfo['store_id'],
                    'log.is_revoke'=>2,
                    //'log.pay_status'=>2//支付状态2已支付 TODO上线需要打开查询已支付
                ])
                ->leftJoin('qt_users u','log.sale_id = u.id')
                ->whereTime('log.create_time',$time)
                ->group('log.sale_id')
                ->paginate($limit)->each(function ($item) use ($userInfo){
                   $item['num'] = Db::name('qt_viper_card_recharge_log')->where([
                        'store_id'=>$userInfo['store_id'],
                        'is_revoke'=>2,
                        //'log.pay_status'=>2//支付状态2已支付 TODO上线需要打开查询已支付
                    ])->where('sale_id',$item['sale_id'])->group('viper_card_id')->count();
                    return $item;
                });

            $this->result('查询成功',$result,200);
        }
    }


    /**
     * 会员卡消费统计
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function storeUserConsumption(){
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if(!isset($getData['time'])|| empty($getData['time'])){
                $this->result('缺少时间类型', '', 204);
            }
            $limit = !empty($getData['limit']) ? $getData['limit'] : 10;
            $userInfo = $this->userInfo;
            //下单时间
            if ($getData['time'] == '1') {
                //今天
                $time = 'today';
            }
            if ($getData['time'] == '2') {
                //昨天
                $time = 'yesterday';
            }
            //下单时间
            if ($getData['time'] == '3') {
                //本月
                $time = 'month';
            }
            if ($getData['time'] == '4') {
                //上月
                $time = 'last month';
            }
            //if($getData['type'] == '1'){
            //    $type = 'cl.created_time';
            //}
            //if($getData['type'] == '2'){
            //    $type = 'cl.updated_time';
            //}
            $result = Db::name('qt_viper_money_log')
                ->alias('log')
                ->field([
                    'vc.name as nickname',
                    'vc.mobile as phone',
                    'log.viper_card_id',
                ])
                ->leftJoin('qt_viper_card vc','log.viper_card_id=vc.id')
                ->where([
                    'log.type'=>3,//消费
                    'vc.store_id'=>$userInfo['store_id'],
                ])
                ->whereTime('log.create_time',$time)
                ->group('log.viper_card_id')
                ->paginate($limit)->each(function ($item) use ($time){
                    $item['num'] = Db::name('qt_viper_money_log')->where('viper_card_id',$item['viper_card_id'])->whereTime('create_time',$time)->group('order_id')->count();
                    $item['money'] = Db::name('qt_viper_money_log')->where('viper_card_id',$item['viper_card_id'])->whereTime('create_time',$time)->sum('money');
                    return $item;
                });

            $this->result('查询成功',$result,200);
        }
    }

    /**
     * 获取柱状图
     * @param $data
     */
    public function histogram($data){
        foreach ($data as $key=>$value){
            $categories[$key] = $value['name'];
            $money[$key] = $value['value'];
        }
        $result['histogram']['categories'] = $categories;
        $result['histogram']['name'] = '金额';
        $result['histogram']['data'] = $money;
        return $result;
    }


    /**
     * 获取会员卡数据统计
     * @param $storeId[门店ID]
     * @param $time[时间类型] [1 今天 2 昨天 3 本月 4 上月]
     * @param $type [类型] [1会员充值 2 会员消费]
     */
    public function getCardTotal($storeId,$time,$type){
        //会员充值
        if($type == '1'){
            $in = ['1','3'];
        }
        //会员消费
        if($type == '2'){
            $in = ['2'];
        }
        //会员充值
        $result['totalCardPrice'] = Db::name('qt_check_out_pay')
            ->where('store_id',$storeId)
            ->whereIn('type',$in)
            ->where('is_vip',1)
            ->whereTime('create_time', $time)
            ->sum('pay_money');
        $careType = Db::name('qt_viper_type')->where(['store_id'=>$storeId])->select();
        foreach ($careType as $key=>$value){
            $result['series'][$key]['name'] = $value['name'];
            $result['series'][$key]['value'] = Db::name('qt_check_out_pay')->alias('pay')
                ->leftJoin('qt_viper_card card','card.id=pay.viper_card_id')
                ->where('pay.store_id',$storeId)
                ->whereIn('pay.type',$in)
                ->where('pay.is_vip',1)
                ->where('card.viper_type_id',$value['id'])
                ->whereTime('pay.create_time', $time)
                ->sum('pay.pay_money');
        }
        return $result;
    }

}
